/*
 * @Date: 2024-02-21 09:46:35
 * @LastEditors: lyx
 * @LastEditTime: 2024-03-20 09:24:35
 * @FilePath: \taro-template3\src\app.tsx
 * @Description: 
 */
import { useDidShow, useDidHide, useLaunch } from "@tarojs/taro";
import { GlobalDataProvider } from "./store/app";
// import IntlElement from "./components/IntlElement";
import "@nutui/icons-react-taro/dist/style_icon.css";
import Taro from "@tarojs/taro";
import { goToPage } from "./utils/common";
import { checkAuth } from "./lib/auth";
import { Provider } from "react-redux";
import { store } from "./store";
require('@/utils/EncoderDecoderTogether.min.js')
// 全局样式
import "./app.scss";
// 国际化
import "./i18next";
import { fetchRefreshToken } from "./pages/index/services";
// import { ChatContextProvider } from "./pages/index/hooks/store";
let intervalTimer: any = null

function App(props) {
  useLaunch(() => {
    // 定时刷新 token
    if (intervalTimer) clearInterval(intervalTimer)
    intervalTimer = setInterval(async () => {
      await fetchRefreshToken()
    }, 1000 * 60 * 60);
  });

  // 对应 onShow
  useDidShow(() => {
    checkAuth();
    // TODO: 授权
    Taro.authorize({
      scope: "scope.record",
      success() {
        // 用户已同意授权
        console.log("用户已同意授权");
      },
      fail() {
        // 用户拒绝了授权
        console.log("用户拒绝了授权");
        Taro.showModal({
          title: "提示",
          content: "需要获取录音权限，请点击确定进入设置页面进行授权。",
          showCancel: false,
          success(res) {
            if (res.confirm) {
              Taro.openSetting({
                success(res) {
                  console.log(res.authSetting);
                  // 用户在设置页面设置了录音权限
                  if (res.authSetting["scope.record"]) {
                    console.log("用户在设置页面设置了录音权限");
                  } else {
                    console.log("用户在设置页面未设置录音权限");
                  }
                },
              });
            }
          },
        });
      },
    });
  });

  // 对应 onHide
  useDidHide(() => { });
  return (
    <Provider store={store}>
      <GlobalDataProvider>
        {/* <ChatContextProvider> */}
        {/* <IntlElement> */}
        {props.children}
        {/* </IntlElement> */}
        {/* </ChatContextProvider> */}
      </GlobalDataProvider>
    </Provider>
  );
}

export default App;
